package com.jxj.study.allConfig.beans;

import org.springframework.stereotype.Component;

import java.util.concurrent.DelayQueue;
import java.util.concurrent.Delayed;

@Component
public class DelayQueueManagers<T extends Delayed> {

    private final DelayQueue<T> delayQueue = new DelayQueue<>();

    /**
     * 获取任务
     */
    public T take() {
        try {
            return delayQueue.take();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 加入到延时队列中
     */
    public void put(T task) {
        delayQueue.put(task);
    }

    /**
     * 取消延时任务
     */
    public boolean remove(T task) {
        return delayQueue.remove(task);
    }

    /**
     * 取消延时任务
     */
    public DelayQueue<T> getQueue() {
        return this.delayQueue;
    }
}
